package com.fresh.bus.chat.ctrl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Controller;

import com.fresh.bus.canoe.command.annotation.CanoeCommand;
import com.fresh.bus.canoe.message.Request;
import com.fresh.bus.canoe.message.Response;
import com.fresh.bus.canoe.session.Session;
import com.fresh.bus.chat.business.ZhiFeiJiService;
import com.fresh.bus.chat.util.MessageUtils;

@Controller("feijiCtrl")
public class ZhiFeijiCtrl extends BaseCtrl {

	@CanoeCommand(value = OpCodes.WRITE_FEIJI)
	public Response writeFeiji(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String id = session.getRole().getId();
		try {
			Response response = new Response(OpCodes.WRITE_FEIJI);
			data.put("room", session.getAttribute("rid"));
			boolean flag =feijiService.createPost(data, id, false);
			response.setData(flag);
			return response;
		} catch (Exception e) {
			log.error("writeFeiji feiji failed...", e);
			this.reportError(session);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.READ_FEIJI)
	public Response readFeiji(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		Response response = new Response(OpCodes.READ_FEIJI);
		String uid = session.getRole().getId();
		long index = 0;
		if (data.get("b") != null) {
			index = Long.valueOf((String) data.get("b"));
		}
		try {
			Map<String, String> m = feijiService.findPost(index, uid);
			if (m != null) {
				response.setData(m);
			} else {
				response.setData(false);
			}
		} catch (Exception e) {
			log.error("readFeiji failed...", e);
			response.setData(e.getMessage());
		}
		return response;
	}

	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.EVALUATE_FEIJI)
	public Response evaluateFeiji(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		Response response = new Response(OpCodes.EVALUATE_FEIJI);
		String uid = session.getRole().getId();
		String fid = (String) data.get("fid");
		int actionType = (Integer) data.get("act");
		try {
			feijiService.updateScore(fid, actionType, uid);
			response.setData(actionType);
		} catch (Exception e) {
			log.error("readFeiji failed...", e);
			response.setData(e.getMessage());
		}
		return response;
	}

	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.STOP_READ_FEIJI)
	public Response stopRead(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		session.removeAttribute("isWatching");
		return null;
	}

	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.READ_MY_FEIJI)
	public Response queryMyPost(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String uid = session.getRole().getId();
		Response response = new Response(OpCodes.READ_MY_FEIJI);
		try {
			List<Map<String, String>> result = feijiService.readPostByUser(uid);
			if (result != null) {
				response.setData(result.toArray());
			} else {
				response.setData(false);
			}
		} catch (Exception e) {
			log.error("queryMyPost " + uid + "failed...", e);
			response.setData(false);
		}
		return response;
	}

	@CanoeCommand(value = OpCodes.GET_FEIJI)
	public Response getFeiji(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String id = session.getRole().getId();
		Response response = new Response(OpCodes.GET_FEIJI);
		String fid = (String) data.get("id");
		try {
			if (!data.isEmpty()) {
				feijiService.getFeiji(data, id);
				feijiService.updateScore(fid, ZhiFeiJiService.YES, id);
				expService.update(session, data, OpCodes.GET_FEIJI);
			}
			response.setData(true);

		} catch (Exception e) {
			log.error("getFeiji[" + id + "]failed...", e);
			response.setData(e.getMessage());
		}
		return response;
	}

	@CanoeCommand(value = OpCodes.READ_FEIJI_RANK)
	public Response getStarFeiji(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String id = session.getRole().getId();
		Response response = new Response(OpCodes.READ_FEIJI_RANK);
		try {
			response.setData(feijiService.getStarFeiji());
		} catch (Exception e) {
			log.error("getFeiji[" + id + "]failed...", e);
			response.setData(MessageUtils.getErrorMessage());
		}
		return response;
	}

	@CanoeCommand(value = OpCodes.GET_YES_FEIJI_PEOPLE)
	public Response getYesFeijiUsers(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String uid = session.getRole().getId();
		Response response = new Response(OpCodes.GET_YES_FEIJI_PEOPLE);
		try {
			String fid = userService.findUser(uid).get("sendFeiji");
			if (!"".equals(fid)) {
				List<Map<String, String>> l = feijiService
						.getYesFeijiPeople(fid);
				if (l.isEmpty()) {
					response.setData("没有发现喜欢你飞机的人，努力加油吧！");
				} else {
					response.setData(l.toArray());
				}
			} else {
				response.setData("先去投个纸飞机再来这里吧！");
			}
			return response;
		} catch (EmptyResultDataAccessException e) {
			response.setData("先去投个纸飞机再来这里吧！");
			return response;
		} catch (Exception e) {
			log.error("getYesFeijiUsers[" + uid + "]failed...", e);
			reportError(session);
		}
		return null;
	}
}
